package com.sisyphus.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sisyphus.dto.LoginDto;
import com.sisyphus.dto.UserRegisterDto;
import com.sisyphus.entity.User;
import com.sisyphus.entity.UserDetail;
import com.sisyphus.exception.Assert;
import com.sisyphus.mapper.UserMapper;
import com.sisyphus.result.enums.impl.BusinessCode;
import com.sisyphus.service.IUserDetailService;
import com.sisyphus.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sisyphus.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.UUID;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Sisyphus
 * @since 2025-06-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    IUserDetailService userDetailService;
    @Override
    public void register(UserRegisterDto dto) {
        User user = checkUserByPhone(dto.getPhone());
        Assert.error(user != null, BusinessCode.USER_EXISTED);
        User u = new User();
        u.setPhone(dto.getPhone());
        u.setPwd(dto.getPwd());
        u.setUserName(dto.getUserName());
        u.setUserStatus(false);
        u.setUserType("1");
        // u.setIdentityCode();
        // u.getRealName(); // 留空用户自己填
        save(u);

        UserDetail userDetail = new UserDetail();
        userDetail.setId(u.getId());
        userDetail.setPhone(dto.getPhone());
        userDetail.setUserName(dto.getUserName());
        userDetail.setUserCode("UC_" + UUID.randomUUID().toString().substring(8));
        userDetail.setReqCode("RC_" + UUID.randomUUID().toString().substring(8));
        userDetail.setCardBindStatus(true);
        userDetailService.save(userDetail);
    }

    private User checkUserByPhone(String phone) {
        LambdaUpdateWrapper<User> wrapper = Wrappers.lambdaUpdate(User.class).eq(User::getPhone, phone);
        User user = getOne(wrapper);
        return user;
    }

    @Override
    public LoginVo login(LoginDto dto) {
        LoginVo loginVo = dto.getIsPhoneLogin() ? loginByPhone() : loginByUsername(dto);
        return loginVo;
    }

    private LoginVo loginByUsername(LoginDto dto) {
        LambdaUpdateWrapper<User> wrapper = Wrappers.lambdaUpdate(User.class).eq(User::getUserName, dto.getLoginName());
        User user = getOne(wrapper);
        Assert.error(user == null, BusinessCode.USER_NOT_EXIST);

        Assert.error(!dto.getLoginSecret().equals(user.getPwd()), BusinessCode.USER_LOGIN_ERROR);
        return creteLoginVo(user);
    }

    private LoginVo creteLoginVo(User user) {
        return LoginVo.builder()
                .id(user.getId())
                .userName(user.getUserName())
                .phone(user.getPhone())
                .token("")
                .build();
    }

    private LoginVo loginByPhone() {
        return null;
    }
}
